﻿#region Librerias

using System;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;
using ESF.General;
using ESF.WinForms;
using System.Linq;
using System.Collections.Generic;
using ESF.FileProcessing;
using System.Threading.Tasks;
using ESF.WinForms.Elements;
using ESF.General.GUI;

#endregion


namespace ESF.FileHelper.Controls
{
	public partial class uscRenameFiles : BaseUserControl
	{
		#region Eventos

		public event Program.DelegateReturnMenu ReturningMenu;

		#endregion
		

		#region Miembros

		private RenameHelper _RenameHelper;

		#endregion

		
		#region Constructor
		
		public uscRenameFiles() : base ()
		{
			InitializeComponent();

			_RenameHelper = new RenameHelper (ReportProgress);
		}

		#endregion


		#region Functions

		private void DoTryRegEx ()
		{
			if (string.IsNullOrWhiteSpace (txtSample.Text))
			{
				frmWarnMsg = "Diligencie el nombre de un archivo de prueba (incluyendo opcionalmente su extension)";
				txtSample.Focus ();
				return;
			}

			if (string.IsNullOrWhiteSpace (txtRegExRename.Text))
			{
				frmWarnMsg = "Diligencie la expresion regular para renombrar";
				txtRegExRename.Focus ();
				return;
			}

			if (string.IsNullOrWhiteSpace (txtRegExReplace.Text))
			{
				frmWarnMsg = "Diligencie el reemplazo";
				txtRegExReplace.Focus ();
				return;
			}

			try			
			{
				txtTryRegex.Text = RenameHelper.ReNameFilenameRegex (txtSample.Text, txtRegExRename.Text, txtRegExReplace.Text);
			}
			catch (Exception pEx)
			{
				frmErrorMsg = pEx.Message;
			}
		}


		private void ReportProgress(string pMessage = "", int pTotalWorkCount = 0, int pReadyWorkCount = 0, int pTotalErrors = 0, long pItemElapsedMs = 0)
		{
			GUIHelper.SetAdvance(ProgressDialog, pMessage, pTotalWorkCount, pReadyWorkCount, pTotalErrors, pItemElapsedMs);
		}

		#endregion


		#region Button Events

		private void btnDoTryRegEx_Click(object sender, EventArgs e)
		{
			DoTryRegEx ();
		}


		private void btnReturnMenu_Click(object sender, EventArgs e)
		{
			if (ReturningMenu != null)
			{
				ReturningMenu ();
			}
		}


		private void btnRename_Click(object sender, EventArgs e)
		{
			string logFileName;


			/*lnkExecutionLog.Visible = false;

			logFileName = Path.GetTempFileName () + ".txt";

			_RenameHelper.Initialize (chkRenameBySecuence.Checked, 
				txtSample.Text, txtRegExRename.Text, txtExtension.Text, txtTry.Text, logFileName);
				
			Enabled = false;

			ProgressDialog.HideWhenComplete = false;
			ProgressDialog.Show (ParentForm);
			ProgressDialog.Clear ();

			Task.Factory.StartNew (delegate ()
			{
				try
				{
					_RenameHelper.RenameInDirectories (txtFolder.Text,
						new string [] { txtFilterExtension.Text });

					if (File.Exists (logFileName) && new FileInfo (logFileName).Length > 0)
					{
						lnkExecutionLog.Invoke ((Action) delegate () 
						{
							lnkExecutionLog.Visible = true;
							lnkExecutionLog.Tag = logFileName;
						});
					}
					else
					{
						ESF.General.FileHelper.Delete (logFileName);
					}
				}
				catch (Exception pEx)
				{
					LogError (pEx);
				}
				finally
				{
					WinFormsHelper.SetEnabled (this);
				}
			});*/
		}

		#endregion


		#region Link Events

		private void lnkSelectFolder_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
		{
			if (fbdSelectFolder.ShowDialog (this) == DialogResult.OK)
			{
				txtFolder.Text = fbdSelectFolder.SelectedPath;
				return;
			}

			txtFolder.Text = string.Empty;
		}


		private void lnkExecutionLog_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
		{
			Process.Start (lnkExecutionLog.Tag as string);
		}

		#endregion


		#region CheckBox Events

		private void chkRenameBySecuence_CheckedChanged(object sender, EventArgs e)
		{
			txtSample.Enabled = chkRenameBySecuence.Checked;
			txtRegExRename.Enabled = chkRenameBySecuence.Checked;
			txtRegExReplace.Enabled = chkRenameBySecuence.Checked;
		}

		#endregion

	}
}
